﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;

namespace FilStartUps.Admin
{
    public partial class AdminReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetForumReport();
                GetAllMembers();
            }
        }

        private void GetAllMembers()
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var member = (from m in db_connect.Members
                              join p in db_connect.Profiles
                                  on m.memberidPK equals p.memberidFK
                              select new
                              {
                                  memberidPK = m.memberidPK,
                                  profileidFK = p.profileidPK,
                                  emailaddress = m.emailaddress,
                                  datejoined = m.datejoined
                              }).ToList();

                //db_connect.Members
                //            .OrderByDescending(m=>m.memberidPK).ToList();
                if (member.Count() < dgmembers.PageSize)
                {
                    dgmembers.PagerStyle.Visible = false;
                }
                dgmembers.DataSource = member;
                dgmembers.DataBind();

            }
        }

        private void GetForumReport()
        {

            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {

                //join frep in db_connect.ForumReplies on
                //                  ft.forumthreadidPK equals frep.forumthreadidFK
                // let forumtitle =(
                //  from fth in db_connect.ForumThreads
                //  where fth.forumthreadidPK == ft.forumthreadidFK
                //   select fth
                //).FirstOrDefault()

                var report = (from fr in db_connect.ForumReports
                              join ft in db_connect.ForumReplies
                                  on fr.forumreplyidFK equals ft.forumreplyidPK
                              join frep in db_connect.ForumThreads
                              on fr.forumthreadidFK equals frep.forumthreadidPK
                              select new
                              {
                                  forumreplyidPK = ft.forumreplyidPK,
                                  forumtitle = frep.forumtitle,
                                  forumthreadidPK = fr.forumthreadidFK,
                                  forumreplyid = ft.forumreplyidPK,
                                  replydescription = ft.replydescription,
                                  reportdescription = fr.reportmessage,
                                  datereported = fr.datereported
                              }).OrderByDescending(c => c.datereported).ToList();
                if (report.Count() < dgreport.PageSize)
                {
                    dgreport.PagerStyle.Visible = false;
                }

                dgreport.DataSource = report;
                dgreport.DataBind();
            }


        }
        public string truncate(string desc)
        {
            string desc_;
            try
            {
                desc_ = desc.ToString().Substring(0, 100) + "...";
            }
            catch
            {
                desc_ = desc;
            }
            return desc_;
        }
        protected void dgreport_ItemCommand(object sender, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "SelectRow")
            {
                UpdateForumReply(Convert.ToInt32(e.CommandArgument));
            }
            else if (e.CommandName == "ForumThread")
            {
                Label lbthread = (Label)dgreport.Items[e.Item.ItemIndex].FindControl("lbforumid");
                Response.Redirect("~/ShowThread.aspx?t=" + lbthread.Text);
            }
        }

        private void UpdateForumReply(int forumreplyid)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var forumreply = db_connect.ForumReplies
                                    .Where(r => r.forumreplyidPK.Equals(forumreplyid)).SingleOrDefault();
                forumreply.forumreplystatus = false;
                db_connect.SaveChanges();
            }
        }

        protected void btnsearchmember_Click(object sender, EventArgs e)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var searchuser = (from m in db_connect.Members
                                  join p in db_connect.Profiles
                                      on m.memberidPK equals p.memberidFK
                                  where m.emailaddress.Contains(txtsearchmember.Text.Trim())
                                  select new
                                {
                                    memberidPK = m.memberidPK,
                                    profileidFK = p.profileidPK,
                                    emailaddress = m.emailaddress,
                                    datejoined = m.datejoined
                                }).ToList();

                dgmembers.DataSource = searchuser;
                dgmembers.DataBind();
            }
        }
    }
}